﻿//模块描述：    导航管理编辑页
//创建人：      
//创建日期：    2010年08月12日 
//修改描述：
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using webnew.Common;
using System.Collections.Generic;

namespace webnew.Systems
{
    public partial class S_navigationEdit : WebPageBase.EditBase

    {
       //string isnav;
        protected void Page_Load(object sender, EventArgs e)
        {
            _ListSrc = "S_navigationList.aspx";            //返回的页面属性 
            //isnav = Request.QueryString["isnav"];       //获取按"保存后继续"的isnav
            txtChildsSort.Style.Add("visibility", "hidden");
            _ListQueryString = "NavType,NavName,RecordCount,CurrentPageIndex";
            ////根据获取的isnav是否存在来判断
            //if (isnav != null)
            //{
            //    //如果isnav为“true”就显示导航的添加页面;否则就显示按钮的添加页面
            //    if (isnav == "True")
            //    {
            //        rbtIsnav.Checked = true;
            //        rbtIsbtn.Checked = false;
            //    }
            //    else
            //    {
            //        rbtIsnav.Checked = false;
            //        rbtIsbtn.Checked = true;
            //    }
            //}

            //根据选择的是导航还是按钮来显示页面
            if (rbtIsnav.Checked == true)
            {
                pnlNav.Visible = true;
                pnlBtn.Visible = false;


            }
            else if (rbtIsbtn.Checked == true)
            {
                pnlBtn.Visible = true;
                pnlNav.Visible = false;
            }
            
            
            if (!IsPostBack)
            {  
                //在第一次加载时绑定下拉框
                //LoadddlPicSrc();// 加载图片选项
                Common.CommonHelper.DataDdlBind(ddlSysType, new BLL.Sys_SystemType().GetconList("导航系统类型"), "TypeName", "TypeName");

                Common.CommonHelper.DataDdlBind(ddlNavParentID, new BLL.Sys_Navigation().GetParentList(), "NaviName", "Id");
                ddlNavParentID.Items.Insert(0, new ListItem("请选择父节点", Guid.Empty.ToString()));
          
                Common.CommonHelper.DataDdlBind(ddlConParentID, new BLL.Sys_Navigation().GetParentList(), "NaviName", "Id");
                ddlConParentID.Items.Insert(0, new ListItem("请选择父节点", Guid.Empty.ToString()));
                
                SetTitle(true, "后台管理>>导航管理");   //设置标题
               
            }
           
        }
        ///// <summary>
        ///// 加载图片选项
        ///// </summary>
        //public void LoadddlPicSrc()
        //{
        //    for (int i =0; i < 21; i++)
        //    {
        //        ddlNavPicSrc.Items.Add(new ListItem(i.ToString(), i.ToString()));
        //        ddlConPicSrc.Items.Add(new ListItem(i.ToString(), i.ToString()));
        //    }
        //}

        /// <summary>
        /// 根据单位的ID显示数据
        /// </summary>
        protected override void ShowData()
        {
           
                Model.Sys_NavigationInfo model = new BLL.Sys_Navigation().GetList(_RecordId);

                listChilds.DataSource = new BLL.Sys_Navigation().GetChildList(_RecordId);
                rbtIsnav.Checked = model.IsPage;                //是导航或者按钮
                rbtIsbtn.Checked = !(model.IsPage);

                if (rbtIsnav.Checked == true)  //导航
                {
                    rbtIsbtn.Enabled = false;
                    pnlNav.Visible = true;
                    pnlBtn.Visible = false;
                    txtNaviName.Text = model.NaviName;              //导航名
                    txtClassName.Text = model.ClassName;            //页面权限名
                    txtMoudelTitle.Text = model.MoudelTitle;   //模块标题
                    txtPageSrc.Text = model.pagesrc;                //链接路径
                    hfNavpic.Value = model.picsrc;         //图标
                    imgNavPicSrc.Src = "../App_Themes/Images/Icon/" + model.picsrc + ".gif";
                    ddlNavParentID.SelectedValue = MyConvert.toString(model.ParentID);   //对应父节点 
                    ddlSysType.SelectedValue = model.SysType;
                    Common.CommonHelper.DataLbBind(listChilds, new BLL.Sys_Navigation().GetChildList(_RecordId), "NaviName", "id");  //对应的下级
                    txtNavRemark.Text = model.remark;                //导航备注
                }
                else  //按钮
                {

                    ViewState["currentConID"] = model.ControlID;  //保存当前按钮ID名,用于验证判断               
                    rbtIsnav.Enabled = false;
                    pnlNav.Visible = false;
                    pnlBtn.Visible = true;
                    txtConName.Text = model.NaviName;                                 //按钮名
                    hfConpic.Value = model.picsrc;         //图标
                    imgConPicSrc.Src = "../App_Themes/Images/Icon/" + model.picsrc + ".gif";
                    txtConID.Text = model.ControlID;                                   //按钮ID
                    txtFunction.Text = model.FunctionName;                             //事件名
                    ddlConParentID.SelectedValue = MyConvert.toString(model.ParentID);  //对应页面
                    txtConRemark.Text = model.remark;                                   //按钮备注                                
                }
            
            
               

           
        }

        /// <summary>
        /// 重写验证控件的事件
        /// </summary>
        /// <returns></returns>
        protected override List<PageValid> _SetValidData()
        {
            List<PageValid> lst = new List<PageValid>();
            //导航
            if (rbtIsnav.Checked == true)
            {
                lst.Add(new PageValid("导航名称", txtNaviName.Text, PageValid.validType.CommonValid, false, 25));
                lst.Add(new PageValid("链接路径", txtPageSrc.Text, PageValid.validType.CommonValid, true, 100));
                lst.Add(new PageValid("模块权限名", txtClassName.Text, PageValid.validType.CommonValid, true, 3000));
                lst.Add(new PageValid("备注", this.txtNavRemark.Text, PageValid.validType.CommonValid, true, 100));

            }
            //按钮
            else
            {

                lst.Add(new PageValid("按钮名称", txtConName.Text, PageValid.validType.CommonValid, false, 25));
                lst.Add(new PageValid("按钮ID", txtConID.Text, PageValid.validType.CommonValid, false, 25));
                lst.Add(new PageValid("事件名", txtFunction.Text, PageValid.validType.CommonValid, false, 25));
                lst.Add(new PageValid("对应页面", "00000000-0000-0000-0000-000000000000", ddlConParentID.SelectedValue, PageValid.validType.NoSameValid, "必须选择"));
                //判断是否存在相同的按钮ID
                if (_RecordId != null) //修改
                {
                    //判断用户在修改状态下是否修改过按钮ID,没修改过 则跳过验证
                    if (ViewState["currentConID"].ToString() != txtConID.Text)
                    {
                        lst.Add(new PageValid("按钮ID", "1",
                            new BLL.Sys_Navigation().ExistConID(txtConID.Text, ddlConParentID.SelectedValue),
                            PageValid.validType.NoSameValid, "已存在,请重新输入"));
                    }
                }
                else //添加
                {
                    lst.Add(new PageValid("按钮ID", "1",
                               new BLL.Sys_Navigation().ExistConID(txtConID.Text, ddlConParentID.SelectedValue),
                               PageValid.validType.NoSameValid, "已存在,请重新输入"));
                }


            }
            return lst;

        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns>成功则返回增加或者修改的行数</returns>
        protected override int SaveData()
        {
            string error = "";                   //获取错误信息
            //修改
            if (_RecordId != null)
            {
                BLL.Sys_Navigation bll = new webnew.BLL.Sys_Navigation();
                Model.Sys_NavigationInfo model = new Model.Sys_NavigationInfo();
                model.Id = _RecordId;
                if (rbtIsnav.Checked == true)
                {
                    model.IsPage = rbtIsnav.Checked;                //导航

                    model.NaviName = txtNaviName.Text;              //导航名
                    model.ClassName = txtClassName.Text;            //页面权限名
                    model.MoudelTitle = txtMoudelTitle.Text;         //模块标题
                    model.pagesrc = txtPageSrc.Text;                //链接路径
                    //model.picsrc = ddlNavPicSrc.SelectedValue;         //图标
                    model.picsrc = hfNavpic.Value;
                    model.ParentID = ddlNavParentID.SelectedValue;   //对应父节
                    model.SysType = ddlSysType.SelectedValue;
                    //如果有子节点，就修改全部子节点
                    if (txtChildsSort.Text != string.Empty)
                    {
                        string[] strChildsSort = txtChildsSort.Text.Split(',');
                        for (int i = 0; i < strChildsSort.Length - 1; i++)
                        {
                            string id = strChildsSort[i];
                            bll.SetChildSort(id, i + 1);
                        }
                    }
                    model.remark = txtNavRemark.Text;                                   //导航备注
                }
                else
                {
                    model.IsPage = rbtIsnav.Checked;                 //按钮
                    model.NaviName = txtConName.Text;                                 //按钮名
                    model.ControlID = txtConID.Text;                                   //按钮ID
                    model.ParentID = ddlConParentID.SelectedValue;   //对应页面
                    //model.picsrc = ddlConPicSrc.SelectedValue;         //图标
                    model.picsrc = hfConpic.Value; //图标
                    model.FunctionName = txtFunction.Text;       //按钮事件名
                    model.remark = txtConRemark.Text;                                 //按钮备注
                    //如果有子节点，就修改全部子节点
                    if (txtChildsSort.Text != string.Empty)
                    {
                        string[] strChildsSort = txtChildsSort.Text.Split(',');
                        for (int i = 0; i < strChildsSort.Length; i++)
                        {
                            string id = strChildsSort[i];
                            bll.SetChildSort(id, i + 1);
                        }
                    }
                }
                //_SaveStaySrc = "S_navigationEdit.aspx?isnav="+rbtIsnav.Checked+" ";  

                //----------系统日志model

                Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
                modelLog.UserID = UserInfor.Id;                  //用户ID
                modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
                modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
                modelLog.CreateDate = DateTime.Now;           //创建时间

               


                
                    try
                    {
                        return bll.Update(model, rbtIsnav.Checked);
                    }
                    catch (Exception e)
                    {
                        error = e.ToString();
                    }
                    finally
                    {
                        //添加一条系统日志
                        if (rbtIsnav.Checked == true)
                        {
                            new BLL.Sys_Log().AddSysLog(error, "修改", modelLog, "系统导航", "名称，导航名称为" + txtNaviName.Text);
                        }
                        else
                            new BLL.Sys_Log().AddSysLog(error, "修改", modelLog,ddlConParentID.SelectedItem.Text + "下的按钮", "名称，按钮名称为" + txtConName.Text);

                    }
                
            }
            //添加
            else
            {
                Model.Sys_NavigationInfo model = new Model.Sys_NavigationInfo();
                BLL.Sys_Navigation bll = new webnew.BLL.Sys_Navigation();
                if (rbtIsnav.Checked == true)
                {
                    model.IsPage = rbtIsnav.Checked;                //导航

                    model.NaviName = txtNaviName.Text;              //导航名
                    model.ClassName = txtClassName.Text;            //页面权限名
                    model.MoudelTitle = txtMoudelTitle.Text;         //模块标题
                    model.pagesrc = txtPageSrc.Text;                //链接路径
                    model.picsrc = hfNavpic.Value;         //图标
                    model.ParentID = ddlNavParentID.SelectedValue;   //对应根结点
                    model.SysType = ddlSysType.SelectedValue;
                    model.Sort = new BLL.Sys_Navigation().GetChildNewSort(ddlNavParentID.SelectedValue);  //导航序号
                    model.remark = txtNavRemark.Text;                                   //导航备注
                    model.isdel = 0;
                }


                else
                {
                    model.IsPage = rbtIsnav.Checked;                 //按钮
                    model.NaviName = txtConName.Text.Trim();                                 //按钮名
                    model.ControlID = txtConID.Text.Trim();                                   //按钮ID
                    model.ParentID = ddlConParentID.SelectedValue; //对应页面
                    model.picsrc = hfConpic.Value;        //图标
                    model.FunctionName = txtFunction.Text.Trim();       //按钮事件名
                    model.Sort = new BLL.Sys_Navigation().GetChildNewSort(ddlConParentID.SelectedValue);  //按钮序号
                    model.remark = txtConRemark.Text;                                 //按钮备注
                    model.isdel = 0;
                }

                //}

                //_SaveStaySrc = "S_navigationEdit.aspx?isnav=" + rbtIsnav.Checked +"";
                  //----------系统日志model

                Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
                modelLog.UserID = UserInfor.Id;                  //用户ID
                modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
                modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
                modelLog.CreateDate = DateTime.Now;           //创建时间

                


                
                    try
                    {
                       return bll.Add(model, rbtIsnav.Checked);
                    }
                    catch (Exception e)
                    {
                        error = e.ToString();
                    }
                    finally
                    {
                        //添加一条系统日志
                        if (rbtIsnav.Checked == true)
                        {
                            new BLL.Sys_Log().AddSysLog(error, "添加", modelLog, "系统导航", "导航，导航名称为" + txtNaviName.Text);
                        }
                        else
                            new BLL.Sys_Log().AddSysLog(error, "添加", modelLog,ddlConParentID.SelectedItem.Text + "下的按钮", "按钮，按钮名称为" + txtConName.Text);

                    }

            }

            //弹出文本框成功或者失败
            if (error == "") { return 1; }       //成功
            else { return 0; }                   //失败

            
        }


       

    }
}
